﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace iLangl.BusinessModel.DAL.XMLStorage
{
	public class PackageDAL : BaseStorageDAL, IPackageDAL
	{

		#region IPackageDAL Members

		public void SavePackage(IPackage package, IDbTransaction transaction)
		{
			dtoXmlStorage.PackageRow packageRow = Storage.Package.FindById(package.Id);

			bool isNew = (packageRow == null);

			if (isNew)
			{
				packageRow = Storage.Package.NewPackageRow();
			}

			packageRow.DateReceived = package.ReceivedTime;
			packageRow.DateSent = package.SentTime;
			packageRow.DeliveryPackageFileName = package.DeliveryPackageFileName;
			packageRow.Id = package.Id;
			packageRow.IsSent = package.IsSent;
			packageRow.IsSuccess = true;
			packageRow.IsSync = package.IsSynced();
			packageRow.License = package.PluginLiceneKey;
			packageRow.Session = package.SessionId;

			if (isNew)
			{
				Storage.Package.AddPackageRow(packageRow);
			}

			if (transaction == null)
			{
				AcceptChanges();
			}
		}

		#endregion
	}
}
